package com.rp.system.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rp.common.utils.JsonUtil;
import com.rp.common.utils.RpUtil;
import com.rp.controller.RpController;
import com.rp.system.po.User;
import com.rp.system.service.UserInfoService;

@Controller
@RequestMapping("/rp/system/userInfoMng/")
public class UserInfoController extends RpController {

	@Autowired
	private UserInfoService userInfoService;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 显示用户中心
	 * @param userId
	 * @return
	 */
	@RequestMapping("showUserCenter")
	public ModelAndView showUserCenter(){
		if(!RpUtil.isNullOrEmpty(User.getCurrentUser())){
			model.put("userId", User.getCurrentUser().getId());
		}
		return forward("rp/system/userInfoMng/userCenter.jsp");
	}
	
	@ResponseBody
	@RequestMapping("getCurrentUserInfo")
	public Map<String, Object> getCurrentUserInfo(String userId){
		String currentUserId = "";
		
		if(RpUtil.isNullOrEmpty(userId)){
			currentUserId = User.getCurrentUser().getId();
		}else{
			currentUserId = userId;
		}
		Map<String, Object> userInfo = 
				jdbcTemplate.queryForMap("select u.realname,u.remark,u.username,ui.* from rp_system_users u left join rp_system_userinfos ui on u.id = ui.userId where u.id = ?", currentUserId);
		
		return userInfo;
	}
	
	@ResponseBody
	@RequestMapping("updateUserInfo")
	public String updateUserInfo(String json, String userId){
		
		Map<String, Object> map = JsonUtil.jsonToMap(json); 
		
		if(RpUtil.isNullOrEmpty((String) map.get("id"))){
			userInfoService.saveUserInfo(map, userId);
		}else{
			userInfoService.updateUserInfo(map, userId);
		}
		
		return SUCCESS;
	}
	
	
}
